package com.cash.app.custom

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.cash.app.R
import com.cash.app.utils.hide

/**
 * Custom view for displaying contact information with copy functionality
 */
class ContactInfoView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val binding by lazy {
        LayoutInflater.from(context).inflate(R.layout.view_contact_info, this, true)
    }

    private val emailLl = binding.findViewById<LinearLayout>(R.id.email_ll)
    private val tvEmail = binding.findViewById<TextView>(R.id.tvEmail)
    private val wa1_ll = binding.findViewById<LinearLayout>(R.id.wa1_ll)
    private val tvWhatsApp = binding.findViewById<TextView>(R.id.tvWhatsApp)
    private val wa2_ll = binding.findViewById<LinearLayout>(R.id.wa2_ll)
    private val tvWhatsApp2 = binding.findViewById<TextView>(R.id.tvWhatsApp2)

    init {
        setupClickListeners()
    }

    private fun setupClickListeners() {
        tvEmail.setOnClickListener { copyToClipboard("Email", tvEmail.text.toString()) }
        tvWhatsApp.setOnClickListener {
            copyToClipboard(
                "WhatsApp", tvWhatsApp.text.toString()
            )
        }
        tvWhatsApp.setOnClickListener {
            copyToClipboard(
                "WhatsApp2", tvWhatsApp2.text.toString()
            )
        }
    }

    private fun copyToClipboard(label: String, text: String) {
        val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clip = ClipData.newPlainText(label, text)
        clipboard.setPrimaryClip(clip)
        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show()
    }

    fun setContactInfo(email: String, whatsApp: String, whatsApp2: String) {
        tvEmail.text = email
        tvWhatsApp.text = whatsApp
        tvWhatsApp2.text = whatsApp2
        if (email.isEmpty()) {
            emailLl.hide()
        }
        if (whatsApp.isEmpty()) {
            wa1_ll.hide()
        }
        if (whatsApp2.isEmpty()) {
            wa2_ll.hide()
        }
    }
} 